package org.mebusw.remote;

import org.mebusw.dao.IUserDAO;
import org.mebusw.dao.factory.DAOFactory;
import org.mebusw.dbc.DBConnInfo;
import org.mebusw.vo.User;

public class UserService {
	protected int onlineUserCount = 0;
	public UserService()	{
		System.out.println("UserService.UserService()");
		DBConnInfo.readFromFile();
	}

	public User login(String email, String password) {
		System.out.println("login() on server of " + email);
		IUserDAO dao = DAOFactory.getIUserDAOInstance();
		User user;

		try {
			user = dao.findByEmail(email);
			
			// TODO password is not verified!!!.
			if (null != user) {
				//TODO use supervision to get the exact count
				onlineUserCount++;
				System.out.println("login() success uid= " + user.getUserId());
				return user;
			} else {
				System.out.println("failed to login as: " + email);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean register(String userName, String password, String email) {
		System.out.println("register() on server of " + userName);
		IUserDAO dao = DAOFactory.getIUserDAOInstance();
		User user = new User();
		user.setUsername(userName);
		user.setPassword(password);
		user.setScore(0);
		user.setReg_date(new java.sql.Date(System.currentTimeMillis()));
		user.setEmail(email);

		try {
			return dao.doCreate(user);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public int getOnlineUserCount() {
		return onlineUserCount;
	}

	public void setOnlineUserCount(int onlineUserCount) {
		this.onlineUserCount = onlineUserCount;
	}
}
